<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row v-show="showSearch">
				<el-form :model="state.queryForm" ref="queryRef" :inline="true" >
					<el-form-item prop="search" :label="$t('common.search')">
						<el-input
							v-model="state.queryForm.search"
							:placeholder="$t('Prod.input') + $t('Prod.name') + '/' + $t('Prod.subTitle') + '/' + $t('Prod.sn') + '/' + $t('Prod.model')"
							clearable
							@clear="getDataList"
							style="width: 350px"
              @keydown.enter.prevent="getDataList"
						/>
					</el-form-item>

					<el-form-item :label="$t('Prod.shopId')" prop="shopId">
						<ShopSelect v-model="state.queryForm.shopId" @clear="getDataList"></ShopSelect>
					</el-form-item>
					<el-form-item :label="$t('Prod.categoryId')" prop="categoryId">
						<SelectTree v-model="state.queryForm.categoryId"></SelectTree>
					</el-form-item>

					<el-form-item :label="$t('Prod.isSale')" prop="isSale">
						<el-select v-model="state.queryForm.isSale" :placeholder="$t('Prod.inputIsSaleTip')" clearable filterable @clear="getDataList">
							<el-option :value="item.value" :label="item.label" v-for="(item, index) in yes_no_bool" :key="index"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item :label="$t('Prod.isCustomizable')" prop="isCustomizable">
						<el-select
							v-model="state.queryForm.isCustomizable"
							:placeholder="$t('Prod.inputIsCustomizableTip')"
							clearable
							filterable
							@clear="getDataList"
						>
							<el-option :value="item.value" :label="item.label" v-for="(item, index) in yes_no_bool" :key="index"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item :label="$t('Prod.isRecommend')" prop="isRecommend">
						<el-select v-model="state.queryForm.isRecommend" :placeholder="$t('Prod.inputIsRecommendTip')" clearable filterable @clear="getDataList">
							<el-option :value="item.value" :label="item.label" v-for="(item, index) in yes_no_bool" :key="index"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item :label="$t('Prod.isHot')" prop="isHot">
						<el-select v-model="state.queryForm.isHot" :placeholder="$t('Prod.inputIsHotTip')" clearable filterable @clear="getDataList">
							<el-option :value="item.value" :label="item.label" v-for="(item, index) in yes_no_bool" :key="index"></el-option>
						</el-select>
					</el-form-item>

					<el-form-item>
						<el-button icon="search" type="primary" @click="getDataList">
							{{ $t('common.queryBtn') }}
						</el-button>
						<el-button icon="Refresh" @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="mb8" style="width: 100%">
					<el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()" v-auth="'bizs_Prod_add'">
						{{ $t('common.addBtn') }}
					</el-button>
					<el-button plain :disabled="multiple" icon="Delete" type="primary" v-auth="'bizs_Prod_del'" @click="handleDelete(selectObjs)">
						{{ $t('common.delBtn') }}
					</el-button>
					<right-toolbar
						v-model:showSearch="showSearch"
						:export="'bizs_Prod_export'"
						@exportExcel="exportExcel"
						class="ml10 mr20"
						style="float: right"
						@queryTable="getDataList"
					></right-toolbar>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
				@sort-change="sortChangeHandle"
			>
				<el-table-column type="selection" width="40" />
				<!--<el-table-column type="index" label="#" width="40" />-->
				<el-table-column prop="id" :label="$t('Prod.prodId')" show-overflow-tooltip min-width="150">
					<template #default="{ row }">
						{{ row.id }}
						<SvgIcon name="ele-CopyDocument" class="icon_copy" @click="copyText(row.id)" />
					</template>
				</el-table-column>
				<el-table-column prop="name" :label="$t('Prod.name')" sortable="custom" show-overflow-tooltip min-width="200">
          <template #default="scope">
            <card-info-col :icon="scope.row.pic" :name="scope.row.name" shape="square" size="large"/>
          </template>
        </el-table-column>
				<el-table-column prop="brief" :label="$t('Prod.subTitle')" show-overflow-tooltip />
				<el-table-column prop="categoryName" :label="$t('Prod.categoryId')" show-overflow-tooltip />
        <!--<el-table-column prop="categoryId" :label="$t('Prod.categoryId')" show-overflow-tooltip />-->
        <!-- <el-table-column prop="categoryPath" :label="$t('Prod.categoryPath')" show-overflow-tooltip /> -->
        <el-table-column prop="shopName" :label="$t('Prod.shopId')" show-overflow-tooltip />
				<el-table-column prop="originAmount" :label="$t('Prod.originAmount')" sortable="custom" show-overflow-tooltip>
					<template #default="{ row }">
						{{ $amountFormat(row.originAmount) }}
					</template>
				</el-table-column>
				<el-table-column prop="amount" :label="$t('Prod.amount')" sortable="custom" show-overflow-tooltip>
					<template #default="{ row }">
						{{ $amountFormat(row.amount) }}
					</template>
				</el-table-column>
        <!--<el-table-column prop="sn" :label="$t('Prod.sn')" show-overflow-tooltip />-->
				<!--<el-table-column prop="model" :label="$t('Prod.model')" show-overflow-tooltip />-->
				<el-table-column prop="saleNum" :label="$t('Prod.saleNum')" sortable="custom" show-overflow-tooltip />
				<el-table-column prop="totalStocks" :label="$t('Prod.totalStocks')" sortable="custom" show-overflow-tooltip />
				<el-table-column prop="isSale" :label="$t('Prod.isSale')" show-overflow-tooltip>
					<template #default="scope">
						<dict-tag :options="yes_no_bool" :value="scope.row.isSale"></dict-tag>
					</template>
				</el-table-column>
				<el-table-column prop="isCustomizable" :label="$t('Prod.isCustomizable')" show-overflow-tooltip>
					<template #default="scope">
						<dict-tag :options="yes_no_bool" :value="scope.row.isCustomizable"></dict-tag>
					</template>
				</el-table-column>
				<el-table-column prop="isRecommend" :label="$t('Prod.isRecommend')" show-overflow-tooltip>
					<template #default="scope">
						<dict-tag :options="yes_no_bool" :value="scope.row.isRecommend"></dict-tag>
					</template>
				</el-table-column>
				<el-table-column prop="isHot" :label="$t('Prod.isHot')" show-overflow-tooltip>
					<template #default="scope">
						<dict-tag :options="yes_no_bool" :value="scope.row.isHot"></dict-tag>
					</template>
				</el-table-column>
				<!-- <el-table-column prop="deliveryType" :label="$t('Prod.deliveryType')" show-overflow-tooltip>
          <template #default="{ row }">
            <dict-tag :options="[{ label: '物流', value: 1 }, { label: '其他', value: 0   }]" :value="row.deliveryType"></dict-tag>
          </template>
        </el-table-column> -->
				<!-- <el-table-column prop="deliveryAddress" :label="$t('Prod.deliveryAddress')" show-overflow-tooltip />
        <el-table-column prop="deliveryCycle" :label="$t('Prod.deliveryCycle')" show-overflow-tooltip /> -->
				<el-table-column prop="sort" :label="$t('Prod.sort')" sortable="custom" show-overflow-tooltip />
				<el-table-column prop="createBy" :label="$t('Prod.createBy')" show-overflow-tooltip />
				<el-table-column prop="createTime" :label="$t('Prod.createTime')" show-overflow-tooltip />
				<el-table-column :label="$t('common.action')" width="150">
					<template #default="scope">
						<el-button icon="edit-pen" text type="primary" v-auth="'bizs_Prod_edit'" @click="formDialogRef.openDialog(scope.row.id)">{{
							$t('common.editBtn')
						}}</el-button>
						<el-button icon="delete" text type="primary" v-auth="'bizs_Prod_del'" @click="handleDelete([scope.row.id])">{{
							$t('common.delBtn')
						}}</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>
		<!-- 编辑、新增  -->
		<form-dialog :prodCategoryIdOption="prodCategoryIdOption" ref="formDialogRef" @refresh="getDataList(false)" />
	</div>
</template>

<script setup lang="ts">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { delObjs, getPage } from '/@/api/bizs/Prod';
import { getProdCateListAPI } from '/@/api/bizs/ProdCategory';
import commonFunction from '/@/utils/commonFunction';

// 定义i18n
import { useI18n } from 'vue-i18n';
// 定义查询字典
import { useDict } from '/@/hooks/dict';
import CardInfoCol from "/@/components/biz/card-info-col.vue";

const ShopSelect = defineAsyncComponent(() => import('/@/components/ShopSelect/index.vue'));
const SelectTree = defineAsyncComponent(() => import('/@/components/biz/selectTreeCom.vue'));

const { t } = useI18n();

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));

const { yes_no_bool } = useDict('yes_no_bool');
// 定义变量内容
const formDialogRef = ref();
// 搜索变量
const queryRef = ref();
const showSearch = ref(true);
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);
const { copyText } = commonFunction();

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: getPage,
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 获取店铺id
interface ShopIdOption {
	id: string;
}

// const shopIdOption = ref<ShopIdOption[]>([])
// const getShoIdList = async () => {

//   let res = await getList()
//   shopIdOption.value = res.data
//   // formDialogRef.value.shopIdOption = res.data.records

// }
// getShoIdList()

// 获取分类id

const prodCategoryIdOption = ref<ShopIdOption[]>([]);
const getProdCategoryId = async () => {
	let res = await getProdCateListAPI();
	prodCategoryIdOption.value = res.data;
};

getProdCategoryId();

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	getDataList();
};

// 导出excel
const exportExcel = () => {
	downBlobFile('/bizs/prod/export', Object.assign(state.queryForm, { ids: selectObjs }), 'Prod.xlsx');
};

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}

	try {
		await delObjs(ids);
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
defineExpose({});
</script>
